package com.sunfield.windpower.fragment;

import android.support.v7.widget.LinearLayoutManager;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sunfield.baseframe.base.BaseFragment;
import com.sunfield.baseframe.http.HttpResult;
import com.sunfield.baseframe.util.JsonUtil;
import com.sunfield.windpower.R;
import com.sunfield.windpower.adapter.NewsAdapter;
import com.sunfield.windpower.bean.NewsInfo;
import com.sunfield.windpower.decoration.NewsSpaceDecoration;
import com.sunfield.windpower.http.HttpClient;
import com.sunfield.windpower.util.UserUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

import java.util.List;

/**
 * 门户信息列表页
 * Created by wxl on 2017/2/7.
 */
@EFragment(R.layout.fragment_list)
public class NewsListFragment extends BaseFragment implements OnRefreshListener, OnLoadMoreListener {
    @ViewById
    LRecyclerView rv_list;

    @FragmentArg
    String newsColumnId;
    @FragmentArg
    int index;
    @Bean
    NewsAdapter adapter;
    @Bean
    HttpClient http;

    LRecyclerViewAdapter lAdapter;

    boolean lock;

    @AfterViews
    void init() {

        rv_list.setBackgroundResource(R.color.colorPrimaryDark);
        rv_list.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_list.addItemDecoration(new NewsSpaceDecoration(getActivity()));
        rv_list.setAdapter(lAdapter = new LRecyclerViewAdapter(adapter));

        rv_list.setOnRefreshListener(this);
        rv_list.setOnLoadMoreListener(this);

//        rv_list.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);

        onRefresh();
    }

    @Override
    public void onRefresh() {
        pageIndex = 0;
        http.getNewsList(UserUtil.getCompanyId(), newsColumnId, pageIndex, pageSize, index);
    }

    @Override
    public void onLoadMore() {
        if(lock){
            return;
        }
        lock = true;
        http.getNewsList(UserUtil.getCompanyId(), newsColumnId, pageIndex, pageSize, index);
    }

    @Override
    public void onHttpResult(HttpResult result) {
        super.onHttpResult(result);
        if(result.match(http, "getNewsList") && result.getConfigKey() == index){
            rv_list.refreshComplete();
            lock = false;
            if(result.isSuccess()){
                List<NewsInfo> newsList = JsonUtil.GetEntityS(JsonUtil.GetJsonArrayByLevel(result.getJson(), "data", "info"), NewsInfo.class);
                if(pageIndex == 0){
                    adapter.clear();
                }
                adapter.addData(newsList);
                pageIndex ++;
                if(newsList.isEmpty()){
                    rv_list.setNoMore(true);
                }
            }
        }
    }
}
